require "Cocos2d"
require "Cocos2dConstants"

-- cclog
cclog = function(...)
    print(string.format(...))
end

-- for CCLuaEngine traceback
function __G__TRACKBACK__(msg)
    cclog("----------------------------------------")
    cclog("LUA ERROR: " .. tostring(msg) .. "\n")
    cclog(debug.traceback())
    cclog("----------------------------------------")
    return msg
end

local function main()
    collectgarbage("collect")
    -- avoid memory leak
    collectgarbage("setpause", 100)
    collectgarbage("setstepmul", 5000)
    cc.Director:getInstance():getOpenGLView():setDesignResolutionSize(480, 320, 0)
	cc.FileUtils:getInstance():addSearchResolutionsOrder("src");
	cc.FileUtils:getInstance():addSearchResolutionsOrder("res");
	local schedulerID = 0
    --support debug
    local targetPlatform = cc.Application:getInstance():getTargetPlatform()
    if (cc.PLATFORM_OS_IPHONE == targetPlatform) or (cc.PLATFORM_OS_IPAD == targetPlatform) or 
       (cc.PLATFORM_OS_ANDROID == targetPlatform) or (cc.PLATFORM_OS_WINDOWS == targetPlatform) or
       (cc.PLATFORM_OS_MAC == targetPlatform) then
        cclog("result is ")
		--require('debugger')()
        
    end
    require "hello2"
    cclog("result is " .. myadd(1, 1))

    ---------------

    local visibleSize = cc.Director:getInstance():getVisibleSize()
    local origin = cc.Director:getInstance():getVisibleOrigin()
    
    local sceneGame = cc.Scene:create()
    local layer = cc.Layer:create()
    local map = cc.TMXTiledMap:create("sample_map1.tmx")
    layer:addChild(map,0,0)
    
    local touchBeginPoint = nil
    local function onTouchBegan(touch, event)
        local location = touch:getLocation()
--        cclog("onTouchBegan: %0.2f, %0.2f", location.x, location.y)
--        local location = cc.Director:getInstance():convertToGL(location)
--        cclog("glLocation: %0.2f, %0.2f", location.x, location.y)
        local nsLocation = layer:convertToNodeSpace(location)
        cclog("nsLocation: %0.2f, %0.2f", nsLocation.x, nsLocation.y)
        touchBeginPoint = {x = location.x, y = location.y} 
        
        local target = event:getCurrentTarget()
        local yellowColor = cc.c3b(233,181,61)
        target:setColor(yellowColor)
        return true
    end
    
    local function onTouchMoved(touch, event)
        local location = touch:getLocation()
        cclog("location: %0.2f, %0.2f", location.x, location.y)
        if(touchBeginPoint) then
            local cx, cy = layer:getPosition()
--            cclog("getPosition: %0.2f, %0.2f", cx, cy)
            layer:setPosition(cx+location.x-touchBeginPoint.x, cy+location.y-touchBeginPoint.y)
            touchBeginPoint = {x=location.x, y=location.y}
        end
    end
    
    local function onTouchEnded(touch, event)
        local location = touch:getLocation()
        touchBeginPoint = nil
    end
    
    local listener = cc.EventListenerTouchOneByOne:create();
    listener:registerScriptHandler(onTouchBegan, cc.Handler.EVENT_TOUCH_BEGAN)
    listener:registerScriptHandler(onTouchMoved, cc.Handler.EVENT_TOUCH_MOVED)
    listener:registerScriptHandler(onTouchEnded, cc.Handler.EVENT_TOUCH_ENDED)
    
--    local dog = cc.Sprite:create("dog.png")
--    dog:setPosition(10, 10)
--    layer:addChild(dog,0,0)
    
--    local mapTile = map:getLayer("Layer 0"):getTileAt(cc.p(0, 10))
    --        map:getLayer("Layer 0"):removeTileAt(cc.p(0, 10))
    --        cclog(mapTile)
--    local mapEventDispatcher = layer:getEventDispatcher()
--    mapEventDispatcher:addEventListenerWithSceneGraphPriority(listener, layer)

    -- run
    sceneGame:addChild(layer,0,0) 
    sceneGame:setScale(0.4,0.4)
    local eventDispatcher = layer:getEventDispatcher()
    eventDispatcher:addEventListenerWithSceneGraphPriority(listener,layer);
	
	if cc.Director:getInstance():getRunningScene() then
		cc.Director:getInstance():replaceScene(sceneGame)
	else
		cc.Director:getInstance():runWithScene(sceneGame)
	end

end


local status, msg = xpcall(main, __G__TRACKBACK__)
if not status then
    error(msg)
end
